from PySide6.QtWidgets import QFrame, QStackedWidget, QHBoxLayout, QWidget
from qfluentwidgets import FlowLayout

from .img_view import ImgAddMarkView, PictureConversionView
from components import AppCard
from resource import OfficeIcon, WordPng


class ImgHomeView(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('ImgHomeView')
        self.layout = FlowLayout(self, needAni=True)

        self.img_mark = AppCard(OfficeIcon.MARK, '图片水印', '给图片添加水印', parent=self)
        self.pictureConversion = AppCard(WordPng.TRANS, '图片转换', '图片格式转换', parent=self)

        self.layout.addWidget(self.img_mark)
        self.layout.addWidget(self.pictureConversion)


class ImgInterface(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('ImgInterface')
        self.stackWidget = QStackedWidget(self)
        self.initUI()
        self.initLayout()
        self.initEvent()

    def initUI(self):
        self.homeFrame = ImgHomeView()
        self.imgAddMarkView = ImgAddMarkView()
        self.pictureConversionView = PictureConversionView()

        self.stackWidget.addWidget(self.homeFrame)
        self.stackWidget.addWidget(self.imgAddMarkView)
        self.stackWidget.addWidget(self.pictureConversionView)

        self.stackWidget.setCurrentIndex(0)

    def initEvent(self):
        # 页面切换
        self.homeFrame.img_mark.clicked.connect(lambda: self.switchTo(self.imgAddMarkView))
        self.homeFrame.pictureConversion.clicked.connect(lambda: self.switchTo(self.pictureConversionView))
        # 返回事件
        self.imgAddMarkView.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.pictureConversionView.returned.connect(lambda: self.switchTo(self.homeFrame))

    def initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 40, 10, 10)
        self.hBox.setSpacing(0)
        self.hBox.addWidget(self.stackWidget)

    def switchTo(self, widget: QWidget):
        self.stackWidget.setCurrentWidget(widget)
